//
//  QSEclipsePlugin.m
//
//   Copyright 2007 Brad Reynolds
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.

#import "QSEclipsePlugin.h"

@implementation QSEclipsePlugin

static QSEclipsePlugin *sharedInstance = nil;
NSImage *eclipseImage;

// returns the user defaults for the plugin
- (NSUserDefaults *) userDefaults {
	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	[self initDefault:defaults key:QSEclipseWorkspaceSearchPath defaultValue:@"~"];
	[self initDefault:defaults key:QSEclipsePath defaultValue:@"/Applications/eclipse"];
	[self initDefault:defaults key:QSEclipseWorkspaceLabel defaultValue:@"%@ Workspace"];
	[self initDefault:defaults key:QSEclipseRunCommand defaultValue:@"'%@/eclipse' -data %@"];
	[self initDefault:defaults key:QSEclipseWorkspaceLogLabel defaultValue:@"%@ Log"];
	[self initDefault:defaults key:QSEclipseWorkspaceSearchPathExcludes defaultValue:[NSArray arrayWithObjects:@"~/Applications", @"~/Desktop", 
		@"~/Documents", @"~/Library", @"~/Movies", @"~/Music", @"~/Pictures", @"~/Public", @"~/Sites", nil]];
	
	return defaults;
}

// Ensures that a value exists for the provided key applying the defaultValue if nil
- (void)initDefault:(NSUserDefaults *) defaults key:(NSString *) key  defaultValue:(id) defaultValue {
	if ([defaults stringForKey:key] == nil) {
		[defaults setObject:defaultValue forKey:key];
	}
}

// Loads the image of the Eclipse icon from the path defined by QSEclipsePath.
//TODO figure out why 'org.eclipse.eclipse' is not loading the image
- (NSImage *) eclipseImage {
	if (eclipseImage == nil) {
		// Loading the Eclipse icon should not be necessary but I'm not sure how to do this correctly in QS.
		//TODO figure out why 'org.eclipse.eclipse' is not loading the image
		NSString *path = [[[self userDefaults] stringForKey:QSEclipsePath] stringByAppendingPathComponent:@"Eclipse.app"];
		eclipseImage = [[NSWorkspace sharedWorkspace] iconForFile:path];
		[eclipseImage retain];
	}
	
	return eclipseImage;
}
 
+ (QSEclipsePlugin*) sharedInstance
{
    @synchronized(self) {
        if (sharedInstance == nil) {
            [[self alloc] init]; // assignment not done here
        }
    }
    return sharedInstance;
}
 
+ (id)allocWithZone:(NSZone *)zone
{
    @synchronized(self) {
        if (sharedInstance == nil) {
            sharedInstance = [super allocWithZone:zone];
            return sharedInstance;  // assignment and return on first allocation
        }
    }
    return nil; //on subsequent allocation attempts return nil
}
 
- (id)copyWithZone:(NSZone *)zone
{
    return self;
}
 
- (id)retain
{
    return self;
}
 
- (unsigned)retainCount
{
    return UINT_MAX;  //denotes an object that cannot be released
}
 
- (void)release
{
    //do nothing
}
 
- (id)autorelease
{
    return self;
}

- (void)dealloc {
    if (eclipseImage != nil) {
		[eclipseImage release];
	}
    [super dealloc];
}

@end
